package org.bits.common.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import org.bits.common.IconConstants;
import org.bits.common.model.ItemTableModel;
import org.jutils.ui.event.*;

/**
 * @param <T>
 */
public class ItemListEditPanel<T>
{
    /**  */
    private ItemListPanel<T> listPanel;
    /**  */
    private final JPanel panel;
    /**  */
    private final ItemTableModel<T> listModel;
    /**  */
    private final ActionListenerList addItemListeners;
    /**  */
    private final ItemActionList<T> editItemListeners;
    /**  */
    private final ItemActionList<T> deleteItemListeners;

    /**
     * @param model
     * @param typeName
     */
    public ItemListEditPanel( ItemTableModel<T> model, String typeName )
    {
        listModel = model;
        listPanel = new ItemListPanel<T>( model );

        panel = createView( listPanel, typeName );

        this.addItemListeners = new ActionListenerList();
        this.editItemListeners = new ItemActionList<>();
        this.deleteItemListeners = new ItemActionList<>();
    }

    /**
     * @param listPanel
     * @param typeName
     * @return
     */
    private JPanel createView( ItemListPanel<T> listPanel, String typeName )
    {
        JPanel panel = new JPanel( new GridBagLayout() );
        JToolBar toolbar = createToolbar( typeName );
        GridBagConstraints constraints;

        constraints = new GridBagConstraints( 0, 0, 1, 1, 1.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(
                0, 0, 0, 0 ), 0, 0 );
        panel.add( toolbar, constraints );

        constraints = new GridBagConstraints( 0, 1, 1, 1, 1.0, 1.0,
            GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets( 0, 0,
                0, 0 ), 0, 0 );
        panel.add( listPanel.getView(), constraints );

        return panel;
    }

    /**
     * @param typeName
     * @return
     */
    private JToolBar createToolbar( String typeName )
    {
        JToolBar toolbar = new JToolBar();
        JButton button;

        button = new JButton(
            IconConstants.loader.getIcon( IconConstants.EDIT_ADD_16 ) );
        button.setToolTipText( "Add " + typeName );
        button.addActionListener( new AddButtonListener() );
        toolbar.add( button );

        button = new JButton(
            IconConstants.loader.getIcon( IconConstants.EDIT_16 ) );
        button.setToolTipText( "Modify " + typeName );
        button.addActionListener( new EditButtonListener() );
        toolbar.add( button );

        button = new JButton(
            IconConstants.loader.getIcon( IconConstants.EDIT_DELETE_16 ) );
        button.setToolTipText( "Delete " + typeName );
        button.addActionListener( new DeleteButtonListener() );
        toolbar.add( button );

        toolbar.setBorderPainted( false );
        toolbar.setFloatable( false );
        toolbar.setRollover( true );

        return toolbar;
    }

    /**
     * @return
     */
    public Component getView()
    {
        return panel;
    }

    /***************************************************************************
     * @param l
     **************************************************************************/
    public void addAddItemListener( ActionListener l )
    {
        addItemListeners.addListener( l );
    }

    /***************************************************************************
     * @param l
     **************************************************************************/
    public void addEditItemListener( ItemActionListener<T> l )
    {
        editItemListeners.addListener( l );

        listPanel.addEditItemListener( l );
    }

    /***************************************************************************
     * @param l
     **************************************************************************/
    public void addRemoveItemListener( ItemActionListener<T> l )
    {
        deleteItemListeners.addListener( l );
    }

    /***************************************************************************
     * @param l
     **************************************************************************/
    public void addItemSelectedListener( ItemActionListener<T> l )
    {
        listPanel.addItemSelectedListener( l );
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class AddButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            addItemListeners.fireListeners( e.getSource(), e.getID(),
                e.getActionCommand() );
        }
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class EditButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            int row = listPanel.getTable().getSelectedRow();

            if( row > -1 )
            {
                T t = listModel.getItem( row );
                editItemListeners.fireListeners( ItemListEditPanel.this, t );
            }
        }
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class DeleteButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            int row = listPanel.getTable().getSelectedRow();

            if( row > -1 )
            {
                T t = listModel.getItem( row );
                deleteItemListeners.fireListeners( ItemListEditPanel.this, t );
            }
        }
    }
}
